package edu.webarchitechture.bank.web.actions;

import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.Preparable;
import edu.webarchitechture.bank.ejb3.services.UserManager;
import edu.webarchitechture.bank.ejb3.domain.User;

import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;

/**
 * User management action.
 *
 * @author Inoka Dissanayaka.
 */
public class UserManagementAction extends ActionSupport implements Preparable {
    private String message;
    private UserManager userManager;

    public void prepare() throws Exception {
        System.out.println("Prepare Called");

        try {
            Context c = new InitialContext();
            userManager = (UserManager) c.lookup("UserManagerBean/local");
        } catch (NamingException ex) {
            System.out.println("NamingException Called");
        } catch (IllegalArgumentException ex) {
            System.out.println("IllegalArgumentException Called");
        }
    }

    public String helloUser() {
        User user = new User();
        user.setUserName("Inoka Dissanayaka");
        user.setPassword("123");

        userManager.saveUser(user);

        message = user.getUserName();

        return SUCCESS;
    }

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }

    public UserManager getUserManager() {
        return userManager;
    }

    public void setUserManager(UserManager userManager) {
        this.userManager = userManager;
    }
}
